<?php
/**
 * User: aka
 * Date: 2018/6/26
 * Time: 17:19
 */

namespace app\api\controller;


use app\common\request\RequestOperation;

class Food
{
    public $requestOperation;
    public $foodModel;

    public function __construct()
    {
        $this->requestOperation = new RequestOperation();
        $this->foodModel = new \app\backstage\model\Food();
    }

    public function __destruct()
    {
        unset($this->requestOperation);
        unset($this->foodModel);
    }

    private function getRequestOperation() : RequestOperation
    {
        return $this->requestOperation;
    }

    private function getFoodModel() : \app\backstage\model\Food
    {
        return $this->foodModel;
    }

    public function classificationAddApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $this->getRequestOperation()->requestValidate('Cassification',$requestData);
        $result = $this->getFoodModel()->classificationAddAction($requestData);
        $result ?
            $this->getRequestOperation()->successCodeToJson(200)
            :
            $this->getRequestOperation()->errorCodeToJson(500);
        ;
    }

    public function classificationSubApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $this->getRequestOperation()->requestValidate('Cassification',$requestData);
        $result = $this->getFoodModel()->classificationSubAction($requestData);
        $result ?
            $this->getRequestOperation()->successCodeToJson(200)
            :
            $this->getRequestOperation()->errorCodeToJson(500);
        ;
    }

    public function classificationEditApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $this->getRequestOperation()->requestValidate('Cassification',$requestData);
        $result = $this->getFoodModel()->classificationEditAction($requestData);
        $result ?
            $this->getRequestOperation()->successCodeToJson(200)
            :
            $this->getRequestOperation()->errorCodeToJson(500);
        ;
    }

    public function classificationStatusOperationApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $returnNum = $this->getFoodModel()->typeStatusOperationAction($requestData);
        $result = $returnNum == 0;
        $result ?
            $this->getRequestOperation()->successCodeToJson(['status'=>200,'id'=>$requestData['id']])
            :
            $this->getRequestOperation()->successCodeToJson(['status'=>201,'id'=>$requestData['id']]);
    }

    public function foodAddApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $this->getRequestOperation()->requestValidate('Food',$requestData);
        $file = request()->file('food_img');

        !empty($file)?:$this->getRequestOperation()->errorCodeToJson('请上传图片');
        if($file){
            $info = $file->move('./static/uploads/');
            if($info){
                $singleImgUrl =  '/static/uploads/'.$info->getSaveName();
                $result = $this->getFoodModel()->foodAddAction($requestData,$singleImgUrl);
                $result ?
                    $this->getRequestOperation()->successCodeToJson(200)
                    :
                    $this->getRequestOperation()->errorCodeToJson(500);
                ;
            }else{
                echo $file->getError();
            }
        }
    }

    public function statusOperationApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $returnNum = $this->getFoodModel()->foodStatusOperationAction($requestData);
        $result = $returnNum == 0;
        $result ?
            $this->getRequestOperation()->successCodeToJson(['status'=>200,'id'=>$requestData['id']])
            :
            $this->getRequestOperation()->successCodeToJson(['status'=>201,'id'=>$requestData['id']]);
    }

    public function topOperationApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $returnNum = $this->getFoodModel()->foodTopOperationAction($requestData);
        $result = $returnNum == 0;
        $result ?
            $this->getRequestOperation()->successCodeToJson(['status'=>200,'id'=>$requestData['id']])
            :
            $this->getRequestOperation()->successCodeToJson(['status'=>201,'id'=>$requestData['id']]);
    }

    public function foodImgEditApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $file = request()->file('edit_food_img');
        !empty($file)?:$this->getRequestOperation()->errorCodeToJson('请上传图片');
        if($file){
            $info = $file->move('./static/uploads/');
            if($info){
                $singleImgUrl =  '/static/uploads/'.$info->getSaveName();
                $result = $this->getFoodModel()->foodImgEditAction($requestData['id'],$singleImgUrl);
                $result ?
                    $this->getRequestOperation()->successCodeToJson(200)
                    :
                    $this->getRequestOperation()->errorCodeToJson(500);
                ;
            }else{
                echo $file->getError();
            }
        }
    }

    public function foodInfoEditApi()
    {
        $requestData = $this->getRequestOperation()->getAllRequestParam();
        $this->getRequestOperation()->requestValidate('Food',$requestData);
        $result = $this->getFoodModel()->foodInfoEditAction($requestData);
        $result ?
            $this->getRequestOperation()->successCodeToJson(200)
            :
            $this->getRequestOperation()->errorCodeToJson(500);
        ;
    }
}